/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import Modele.DAO;
import Modele.Dep;
import Modele.Med;
import Modele.Pays;
import Modele.Spe;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 *
 * @author sio
 */
public class Control extends HttpServlet {

    private Pays p;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        p = new Pays(); //instancie les objets utiles

    }

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     * @throws java.sql.SQLException
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {

        String page = null;
        String uc;

        if (request.getParameter("uc") == null) {
            uc = "null";
        } else {
            uc = request.getParameter("uc");
        }

        switch (uc) {
            case "null":
                page = "index.jsp";
                break;

            case "dep":

                if (request.getParameter("lesDepartement") == null) {
                Collection<Dep> lesDep = new TreeSet();
                lesDep = p.GetLesDeps();
                request.setAttribute("lesDep", lesDep);
                page = "parDep.jsp";

                } else
                {

                    Collection<Med> lesMed = p.getLeDep(request.getParameter("lesDepartement")).getLesMeds();
                    request.setAttribute("lesMed", lesMed);
                    page = "test.jsp";

                }
                break;

            case "nom":

                if (request.getParameter("parNom") == null) {
                
                page = "parNom.jsp";

                }
                else {

                    Collection<Med> lesMedsParNom = new TreeSet();
                    String laRecherche = request.getParameter("parNom");
                    for (Dep d : p.GetLesDeps()) {
                        for (Med m : d.getLesMeds()) {
                            if (m.getNom().startsWith(laRecherche)) {
                                lesMedsParNom.add(m);
                            }
                        }
                    }

                    request.setAttribute("lesMedsParNom", lesMedsParNom);
                    page = "parNom2.jsp";

                }
                break;
                
            case "spe" : 
                if (request.getParameter("lesSpe") == null) {
                Collection <Spe> lesSpe = new TreeSet();
                lesSpe = p.GetLesSpe();
                request.setAttribute("lesSpe", lesSpe);
                page = "parSpe.jsp";
                }
                else {
                    Collection <Med> lesMedParSpe = new TreeSet();
                    Spe laSpe = p.getLaSpe(request.getParameter("lesSpe"));
                    lesMedParSpe = laSpe.getLesMeds();
                    request.setAttribute("lesMedParSpe", lesMedParSpe);
                    page = "parSpe2.jsp";
                }
                break;
                
        }

        request.getRequestDispatcher(page).forward(request, response);

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
